package com.evil.mi.service.impl;

import com.evil.mi.dao.AlternativeVideoRepository;
import com.evil.mi.dao.VideoImgRepository;
import com.evil.mi.dao.VideoRepository;
import com.evil.mi.entity.AlternativeVideoEntity;
import com.evil.mi.entity.PageEntity;
import com.evil.mi.entity.VideoEntity;
import com.evil.mi.enums.VideoStateEnum;
import com.evil.mi.exception.base.BaseException;
import com.evil.mi.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * com.evil.mi.service.impl
 *
 * @author 白雨浓
 * date: 2018/8/20 15:44
 * content:
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoRepository videoRepository;

    @Autowired
    private VideoImgRepository videoImgRepository;

    @Autowired
    private AlternativeVideoRepository alternativeVideoRepository;

    @Override
    public PageEntity<VideoEntity> findVideoPage(int current, int size) {

        if (current <= 0) {
            return new PageEntity<>(current, size, 0, new ArrayList<>());
        }

        List<String> stateList = Arrays.asList(
                VideoStateEnum.INIT.getCode(),
                VideoStateEnum.DOWNLOADING.getCode(),
                VideoStateEnum.FINISH.getCode()
        );

        List<VideoEntity> list = videoRepository.findPage(stateList, (current - 1) * size, size);

        int count = videoRepository.findCount(stateList);

        return new PageEntity<>(current, size, count, list);
    }

    @Override
    public PageEntity<VideoEntity> findVideoAlternativePage(int current, int size) {

        List<String> stateList = Collections.singletonList(VideoStateEnum.ALTERNATIVE.getCode());

        List<VideoEntity> list = videoRepository.findPage(stateList, (current - 1) * size, size);

        int count = videoRepository.findCount(stateList);

        return new PageEntity<>(current, size, count, list);
    }

    @Override
    public VideoEntity findVideoById(int id) {
        return videoRepository.findById(id).orElse(null);
    }

    @Override
    public int saveVideo(VideoEntity entity) {

        entity.setState(VideoStateEnum.INIT);
        entity.setCreateTime(new Date());

        videoRepository.save(entity);
        log.info("存入视频信息>> url:{}, name:{}", entity.getUrl(), entity.getName());

        return entity.getId();
    }

    @Override
    public void modifyVideoState(int id, VideoStateEnum state) {

        VideoEntity entity = videoRepository.findById(id).orElse(null);

        if (entity == null) {
            throw new BaseException("视频不存在");
        }

        entity.setState(state);
        videoRepository.save(entity);
    }

    @Override
    @Transactional
    public void alternativeVideo(int id, boolean option) {

        if (option) {
            this.modifyVideoState(id, VideoStateEnum.ALTERNATIVE);
            alternativeVideoRepository.save(AlternativeVideoEntity.builder().videoId(id).build());
        } else {
            this.modifyVideoState(id, VideoStateEnum.INIT);
            alternativeVideoRepository.deleteByVideoId(id);
        }
    }

    @Override
    @Transactional
    public void removeVideo(int id) {

        try {
            alternativeVideoRepository.deleteByVideoId(id);

            videoImgRepository.deleteByVideoId(id);

            videoRepository.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
